package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.StoreService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.StoreMapper;
import cn.icare.cms.dao.po.Store;
import cn.icare.cms.model.ro.AddStoreRO;
import cn.icare.cms.model.ro.EditStoreRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {

    @Resource
    private StoreMapper storeMapper;

    private final Logger logger = LoggerFactory.getLogger(StoreServiceImpl.class);


    @Override
    public Integer createOne(AddStoreRO ro, String actionUserName) {
        try {
            Store model = new Store();
            model.setName(ro.getName());
            model.setAreaid(ro.getAreaId());
            model.setAddress(ro.getAddress());
            model.setCreator(actionUserName);
            model.setCreatetime(new Date());
            model.setLat(ro.getLat());
            model.setLon(ro.getLon());
            int result = this.storeMapper.insertSelective(model);
            if (result > 0) {
                return model.getId();
            }
        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }
        return 0;
    }

    @Override
    public boolean updateOne(EditStoreRO ro, String actionUserName) {

        try {
            Store model = new Store();
            model.setId(ro.getId());
            model.setName(ro.getName());
            model.setAreaid(ro.getAreaId());
            model.setAddress(ro.getAddress());
            model.setCreator(actionUserName);
            model.setCreatetime(new Date());
            model.setLat(ro.getLat());
            model.setLon(ro.getLon());
            int result = this.storeMapper.updateByPrimaryKeySelective(model);
            if (result > 0) {
                return true;
            }
        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }
        return false;
    }

    @Override
    public boolean deleteOne(Integer id) {
        Integer result = this.storeMapper.deleteByPrimaryKey(id);
        return result > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Store.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.storeMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public Store getOne(Integer id) {
        return this.storeMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(Integer areaId, String name, Integer pageNum, Integer pageSize) {

        MsgDatasVO result = new MsgDatasVO();
        try {
            if (pageSize == 0) {
                Example example = new Example(Store.class);
                Example.Criteria criteria = example.createCriteria();
                if (areaId != null && areaId > 0) {
                    criteria.andEqualTo("areaid", areaId);
                }
                if (StringUtil.isNotEmpty(name)) {
                    criteria.andLike("name", "%" + name + "%");
                }
                List<Store> dataList = this.storeMapper.selectByExample(example);
                result.setTotal(dataList.size());
                result.setDatas(dataList);
            }
            else {
                PageHelper.startPage(pageNum, pageSize);
                Example example = new Example(Store.class);
                Example.Criteria criteria = example.createCriteria();
                if (areaId != null && areaId > 0) {
                    criteria.andEqualTo("areaid", areaId);
                }
                if (StringUtil.isNotEmpty(name)) {
                    criteria.andLike("name", "%" + name + "%");
                }
                List<Store> dataList = this.storeMapper.selectByExample(example);
                PageInfo pageInfo = new PageInfo(dataList);
                result.setTotal(pageInfo.getTotal());
                result.setDatas(dataList);
            }
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }

    @Override
    public boolean isExists(Integer id, Long areaId, String name) {
        Example example = new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        if (areaId>0) {
            criteria.andEqualTo("areaid", areaId);
        }
        criteria.andNotEqualTo("id",id);

        List<Store> dataList = this.storeMapper.selectByExample(example);
        if(dataList!=null && dataList.size()>0)
        {
            return true;
        }
        return false;
    }
}
